package com.ruoyi.base.service.impl;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.CarouselsMapper;
import com.ruoyi.base.domain.Carousels;
import com.ruoyi.base.service.ICarouselsService;

/**
 * 轮播图Service业务层处理
 * 
 * @author 刘大理
 * @date 2025-06-24
 */
@Service
public class CarouselsServiceImpl extends ServiceImpl<CarouselsMapper, Carousels> implements ICarouselsService
{
    @Autowired
    private CarouselsMapper carouselsMapper;

    /**
     * 查询轮播图
     * 
     * @param id 轮播图主键
     * @return 轮播图
     */
    @Override
    public Carousels selectCarouselsById(Long id)
    {
        return carouselsMapper.selectCarouselsById(id);
    }

    /**
     * 查询轮播图列表
     * 
     * @param carousels 轮播图
     * @return 轮播图
     */
    @Override
    public List<Carousels> selectCarouselsList(Carousels carousels)
    {
        return carouselsMapper.selectCarouselsList(carousels);
    }

    /**
     * 新增轮播图
     * 
     * @param carousels 轮播图
     * @return 结果
     */
    @Override
    public int insertCarousels(Carousels carousels)
    {
        return carouselsMapper.insertCarousels(carousels);
    }

    /**
     * 修改轮播图
     * 
     * @param carousels 轮播图
     * @return 结果
     */
    @Override
    public int updateCarousels(Carousels carousels)
    {
        return carouselsMapper.updateCarousels(carousels);
    }

    /**
     * 批量删除轮播图
     * 
     * @param ids 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCarouselsByIds(Long[] ids)
    {
        return carouselsMapper.deleteCarouselsByIds(ids);
    }

    /**
     * 删除轮播图信息
     * 
     * @param id 轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCarouselsById(Long id)
    {
        return carouselsMapper.deleteCarouselsById(id);
    }

    @Value("${server.port}")
    private Integer port;
    @Value("${server.ip}")
    private String ip;
    @Override
    public ResponseEntity carousels() throws UnknownHostException {
        LambdaQueryWrapper<Carousels> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Carousels::getCreatedAt);
        List<Carousels> list = this.list(queryWrapper);
        List<JSONObject> resp = new ArrayList<>();
        for (Carousels carousels:list){
            String[] split = carousels.getImageUrl().split(",");
            for (int i = 1; i < split.length+1; i++) {
                JSONObject item = new JSONObject();
                item.put("image",ip+split[i-1]);
                resp.add(item);
            }
        }
        JSONObject json = new JSONObject();
        json.put("list",resp);
        json.put("page",resp.size());
        return new ResponseEntity().success(json);
    }
}
